package com.wutianyi.favority.bean;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;

import com.wutianyi.favority.constants.Constants;
import com.wutianyi.favority.rel.RelTypes;

/**
 * @author hanjiewu
 *
 */
public class Folder {
	private final Node underlyingNode;

	public Folder(Node underlyingNode) {
		this.underlyingNode = underlyingNode;
	}

	public Node getUnderlyingNode() {
		return underlyingNode;
	}

	public String getName() {
		return (String) underlyingNode.getProperty(Constants.FOLDER_NAME);
	}

	public long getFolderId() {
		return underlyingNode.getId();
	}

	@Override
	public int hashCode() {
		return underlyingNode.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return obj instanceof Folder
				&& ((Folder) obj).getUnderlyingNode().equals(underlyingNode);
	}

	public List<Link> getLinks() {
		Iterable<Relationship> rs = underlyingNode.getRelationships(
				RelTypes.LINKED, Direction.OUTGOING);
		List<Link> links = new ArrayList<Link>();
		for (Relationship r : rs) {
			links.add(new Link(r.getEndNode()));
		}
		return links;
	}
}
